<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <link rel="stylesheet" href="assets/mui/css/mui.min.css">
    <link rel="stylesheet" href="assets/fontAwesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="css/base.css">
    <link rel="stylesheet" href="css/reg.css">
    <script src="assets/zepto/zepto.min.js"></script>
    <script src="assets/mui/js/mui.js"></script>
    <script src="./js/common.js"></script>
    <style>
        .yzm input[type="text"] {
            width: 100px;
            float: left;
        }
        .yzm a {
            height: 40px;
            line-height: 40px;
            color: blue;
            padding-right: 10px;
        }
    </style>
</head>

<body>
    <header class="mui-bar mui-bar-nav header">
        <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left" href="javascript:history.back(-1)"></a>
        <h1 class="mui-title">修改密码</h1>
        <a class="mui-icon mui-icon-search mui-pull-right"></a>

    </header>
    <nav class="mui-bar mui-bar-tab footer">
        <a class="mui-tab-item" href="index.html">
            <span class="mui-icon mui-icon-home"></span>
            <span class="mui-tab-label">首页</span>
        </a>
        <a class="mui-tab-item" href="category.html">
            <span class="mui-icon mui-icon-bars"></span>
            <span class="mui-tab-label">分类</span>
        </a>
        <a class="mui-tab-item" href="cart.html">
            <span class="mui-icon fa fa-shopping-cart"></span>
            <span class="mui-tab-label">购物车</span>
        </a>
        <a class="mui-tab-item" href="user.html">
            <span class="mui-icon mui-icon-person"></span>
            <span class="mui-tab-label">会员中心</span>
        </a>
    </nav>
    <div class="mui-content">
        <form class="mui-input-group">
            <div class="mui-input-row">
                <label>原密码</label>
                <input type="password" name="oldpwd" class="mui-input-password" placeholder="请输入原密码">
            </div>
            <div class="mui-input-row">
                <label>新密码</label>
                <input type="password" name="npwd" class="mui-input-password" placeholder="请输入新密码">
            </div>
            <div class="mui-input-row">
                <label>确认新密码</label>
                <input type="password" name="npwd2" class="mui-input-password" placeholder="请输入新密码">
            </div>
            <div class="mui-input-row yzm">
                <label>验证码</label>
                <input type="text" name="yzm" class="mui-input-clear" placeholder="请输入验证码">
                <a href="#" class="pull-right">获取验证码</a>
            </div>
            <div class="mui-button-row">
                <button type="button" class="changepwd mui-btn mui-btn-primary">确认修改</button>
            </div>
        </form>
    </div>
</body>
<script>
    $(".changepwd").on('tap', function () {
        var oldpwd = $('input[name="oldpwd"]').val()
        var npwd = $('input[name="npwd"]').val()
        var npwd2 = $('input[name="npwd2"]').val()
        var yzm = $('input[name="yzm"]').val()

        if (oldpwd == "") {
            mui.toast("输入原密码")
            return;
        }

        if (npwd == "" || npwd2 == "") {
            mui.toast("输入密码")
            return;
        }

        if (npwd2 == oldpwd) {
            mui.toast("新旧密码不得一致")
            return;
        }

        $.ajax({
            url: '/user/updatePassword',
            type: 'POST',
            beforeSend: function () {
                $('reg').html('正在登陆中.....')
            },
            data: {
                oldPassword: oldpwd,
                newPassword: npwd,
                vCode: yzm
            },
            success: function (res) {
                console.log(res)
                if (res.success) {
                    $('reg').html('修改成功,即将返回登录页面.')
                    setTimeout(() => {
                        $.ajax({
                            url: '/user/logout',
                            type: 'GET',
                            success: function (res) {
                                if (res.success) {
                                    mui.toast('成功退出')
                                    setTimeout(() => {

                                        location.href = 'login.html'
                                    }, 1000);
                                } else if (res.error) {
                                    mui.toast(res.message)
                                }
                            }
                        })
                    }, 1000);
                } else if (res.error) {
                    mui.toast(res.message)
                }
            }
        })
    })
    $('.yzm a').on('tap', function () {
        console.log(1)
        $.ajax({
            url: '/user/vCodeForUpdatePassword',
            type: "GET",
            success: function (res) {
                console.log(res)
            }
        })
    })

</script>

</html>
